import { UC } from '@/api/config/servicePort'
import http from '@/api'
import { ResPage, TreeNode, DeptNode } from '@/api/interface'
import { Dept } from './interface'

// 获取列表
export const listDeptNode = (id?: string) => {
  return http.get<DeptNode[]>(UC + `/dept/node/list`, { id }, { loading: false })
}

// 树型菜单
export function listDeptTable(query?: Dept.Query) {
  return http.get<ResPage<TreeNode>>(UC + '/dept/table', query ? query : { pageable: false })
}

// 获取列表
export const deptTree = (id?: string) => {
  return http.get(UC + `/dept/select/tree`, { id })
}

// 新增
export const addDept = (params: Partial<Dept.Form>) => {
  return http.post(UC + `/dept`, params)
}

// 编辑
export const updateDept = (params: Partial<Dept.Form>) => {
  return http.put(UC + '/dept', params)
}
// 删除
export const removeDept = (id: string) => {
  return http.delete(UC + `/dept`, { id })
}

// 详情
export const getDept = (id: string) => {
  return http.get<Partial<Dept.Form>>(UC + '/dept', { id }, { loading: false })
}

// 新增表单默认值
export const getDefault = (parentId: string) => {
  return http.get<Partial<Dept.Default>>(UC + '/dept/default', { parentId }, { loading: false })
}

// 检查编码是否存在
export function checkExist(newId?: string) {
  return http.get<boolean>(UC + '/dept/exist', { newId }, { loading: false })
}

/**
 * 获取子级
 * @param 编码
 */
export const subDepts = (id?: string) => {
  return http.get<DeptNode[]>(UC + '/dept/child', { id }, { loading: false })
}
